//
//  WPPwdLoginViewController.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/3/30.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPPwdLoginViewController.h"
#import "WPLoginViewModel.h"

@interface WPPwdLoginViewController ()<UITextFieldDelegate>
@property (nonatomic, strong) UIImageView * backImageView;/**< 背景图 */
@property (nonatomic, strong) UIImageView * logoImageView;/**< 图标 */
@property (nonatomic, strong) UITextField * accountTextField;/**< 账号输入框 */
@property (nonatomic, strong) UITextField * pwdTextField;/**< 密码输入框 */
@property (nonatomic, strong) UIButton * protocolBtn;/**< 同意协议按钮 */
@property (nonatomic, strong) UILabel * protocolLabel;/**< 用户协议 */
@property (nonatomic, strong) UITextField * findPwdTextField;/**< 找回密码按钮 */
@property (nonatomic, strong) UIButton * loginBtn;/**< 登录按钮 */
@property (nonatomic, strong) UIButton * msgLoginBtn;/**< 短信登录按钮 */
@property (nonatomic, strong) UIButton * registerBtn;/**< 注册按钮 */
- (void)initializeUserInterface; /**< 初始化用户界面 */

@end

@implementation WPPwdLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeUserInterface];
}
#pragma mark - init
- (void)initializeUserInterface {
    self.navigationController.navigationBarHidden = YES;
    [self.view addSubview:self.backImageView];
    [self.view addSubview:self.logoImageView];
    [self.view addSubview:self.accountTextField];
    [self.view addSubview:self.pwdTextField];
    [self.view addSubview:self.protocolBtn];
    [self.view addSubview:self.protocolLabel];
    [self.view addSubview:self.findPwdTextField];
    [self.view addSubview:self.loginBtn];
//    [self.view addSubview:self.msgLoginBtn];
    [self.view addSubview:self.registerBtn];
    UISwipeGestureRecognizer * gesLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(respondsToLogoImageView)];
    UISwipeGestureRecognizer * rightGes = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(respondsToLogoImageView)];
    gesLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    rightGes.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:gesLeft];
    [self.view addGestureRecognizer:rightGes];
}
#pragma mark - responds events
- (void)textFieldDidChange:(UITextField *)textField {
    if (textField == _accountTextField && textField.text.length > 11) {
        textField.text = [textField.text substringToIndex:11];
    }
    if (textField == _pwdTextField && textField.text.length > 14) {
        textField.text = [textField.text substringToIndex:14];
    }
}
- (void)respondsToLogoImageView {
    [ControllerManager sharedManager].rootViewController.selectedIndex = 0;
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}
- (void)respondToFindPwdBtn {
    [self.view endEditing:YES];
    [self pushToNextController:[[NSClassFromString(@"WPFindPwdViewController") alloc]init] withVCTitle:@"账户密码"];
}
- (void)respondsToProtocolBtn:(UIButton *)sender {
    sender.selected = !sender.selected;
}
- (void)respondsToUserProtocol {
    [self pushToNextController:[[NSClassFromString(@"WPUserProtocolViewController") alloc]init]withVCTitle:@"注册登录协议"];
}
- (void)respondToLoginBtn {
    [self.view endEditing:YES];
    if (_accountTextField.text.length == 0 || ![ConfirmMobileNumber isMobileNumber:_accountTextField.text]) {
        [self initializeAlertControllerWithMessageAndDismiss:@"请输入正确的手机号码"];
        return;
    }
    if (_pwdTextField.text.length < 6) {
        [self initializeAlertControllerWithMessageAndDismiss:@"请输入至少6位数密码"];
        return;
    }
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.removeFromSuperViewOnHide = YES;
    hud.color = COLOR_RGB(76, 76, 76, 0.8);
    hud.labelText = @"请稍候";
    hud.detailsLabelText = @"登录中...";
    __weak typeof(self)weakself = self;
    [WPLoginViewModel loginWithPhone:_accountTextField.text andPwd:_pwdTextField.text andSuccessBlock:^(NSString *result) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [hud hide:YES];
            [ControllerManager sharedManager].rootViewController.selectedIndex = 0;
            [weakself.navigationController dismissViewControllerAnimated:YES completion:^{
            }];
        });
    } andFailureBlock:^(NSString *fail) {
        hud.labelText = @"登陆失败";
        hud.detailsLabelText = fail;
        [hud hide:YES afterDelay:2.0];
    }];
}
- (void)respondToMsgLoginBtn {
    [self.view endEditing:YES];
    [self pushToNextController:[[NSClassFromString(@"WPMsgLoginViewController") alloc]init] withVCTitle:nil];
}
- (void)respondToRegisterBtn {
    [self.view endEditing:YES];
    if (self.navigationController.viewControllers.count > 1) {
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    [self pushToNextController:[[NSClassFromString(@"WPRegisterViewController") alloc] init] withVCTitle:nil];
}
#pragma mark - system protocol
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if (textField == _findPwdTextField) {
        [self pushToNextController:[[NSClassFromString(@"WPFindPwdViewController") alloc]init] withVCTitle:nil];
    }
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    if (textField == _pwdTextField) {
        [self respondToLoginBtn];
    }
    return YES;
}
#pragma mark - getter
- (UIImageView *)backImageView {
    if (!_backImageView) {
        _backImageView = ({
            UIImageView * view = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height)];
            view.image = IMAGE_NAME(@"图层-2.png");
            view;
        });
    }
    return _backImageView;
}
- (UIImageView *)logoImageView {
    if (!_logoImageView) {
        _logoImageView = ({
            UIImageView * view = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40*DHFlexibleHorizontalMutiplier(), 86*DHFlexibleHorizontalMutiplier())];
            view.center = CGPointMake(self.view.center.x, 108*DHFlexibleVerticalMutiplier());
            view.image = IMAGE_NAME(@"loginLogo.png");
            view.userInteractionEnabled = YES;
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(respondsToLogoImageView)];
            [view addGestureRecognizer:tap];
            view;
        });
    }
    return _logoImageView;
}
- (UITextField *)accountTextField {
    if (!_accountTextField) {
        _accountTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(35*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.logoImageView.frame)+115*DHFlexibleVerticalMutiplier(), 250*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            text.leftViewMode = UITextFieldViewModeAlways;
            UIView * leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            UIImageView * userImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 7*DHFlexibleHorizontalMutiplier(), 15*DHFlexibleHorizontalMutiplier(), 15*DHFlexibleHorizontalMutiplier())];
            userImage.image = IMAGE_NAME(@"iconfont-yonghuming.png");
            [leftView addSubview:userImage];
            text.leftView = leftView;
            UIView * line = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(text.bounds)-0.5, text.bounds.size.width, 0.5)];
            line.backgroundColor = [UIColor whiteColor];
            [text addSubview:line];
            text.keyboardType = UIKeyboardTypeNumberPad;
            text.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            text.textColor = [UIColor whiteColor];
            [text addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
            text.clearButtonMode = UITextFieldViewModeWhileEditing;
            NSMutableDictionary *dict = [NSMutableDictionary dictionary]; dict[NSForegroundColorAttributeName] = [UIColor whiteColor]; NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"请输入注册手机号" attributes:dict];
            text.attributedPlaceholder = str;
            text.delegate = self;
            text;
        });
    }
    return _accountTextField;
}
- (UITextField *)pwdTextField {
    if (!_pwdTextField) {
        _pwdTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(35*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.accountTextField.frame)+15*DHFlexibleVerticalMutiplier(), 250*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            text.leftViewMode = UITextFieldViewModeAlways;
            UIView * leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            UIImageView * userImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 9.5*DHFlexibleHorizontalMutiplier(), 13*DHFlexibleHorizontalMutiplier(), 16*DHFlexibleHorizontalMutiplier())];
            userImage.image = IMAGE_NAME(@"iconfont-mima-拷贝");
            [leftView addSubview:userImage];
            text.leftView = leftView;
            UIView * line = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(text.bounds)-0.5, text.bounds.size.width, 0.5)];
            line.backgroundColor = [UIColor whiteColor];
            [text addSubview:line];
            text.keyboardType = UIKeyboardTypeDefault;
            text.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            text.textColor = [UIColor whiteColor];
            text.secureTextEntry = YES;
            [text addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
            text.clearButtonMode = UITextFieldViewModeWhileEditing;
            NSMutableDictionary *dict = [NSMutableDictionary dictionary]; dict[NSForegroundColorAttributeName] = [UIColor whiteColor]; NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"请输入密码" attributes:dict];
            text.attributedPlaceholder = str;
            text.delegate = self;
            text;
        });
    }
    return _pwdTextField;
}
- (UIButton *)protocolBtn {
    if (!_protocolBtn) {
        _protocolBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(35 *DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.pwdTextField.frame)+15*DHFlexibleVerticalMutiplier(), 15*DHFlexibleHorizontalMutiplier(), 15*DHFlexibleHorizontalMutiplier());
            [btn setImage:IMAGE_NAME(@"iconfont_fuxuankuangwxz") forState:UIControlStateNormal];
            [btn setImage:IMAGE_NAME(@"iconfon_fuxuankuang.png") forState:UIControlStateSelected];
            btn.selected = YES;
            [btn addTarget:self action:@selector(respondsToProtocolBtn:) forControlEvents:UIControlEventTouchUpInside];
            btn;

        });
    }
    return _protocolBtn;
}
- (UILabel *)protocolLabel {
    if (!_protocolLabel) {
        _protocolLabel = ({
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(55*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.pwdTextField.frame)+5*DHFlexibleVerticalMutiplier(), 100*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            label.font = [UIFont systemFontOfSize:10*DHFlexibleHorizontalMutiplier()];
            label.textColor = [UIColor whiteColor];
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(respondsToUserProtocol)];
            [label addGestureRecognizer:tap];
            label.userInteractionEnabled = YES;
            label.text = @"同意协议";
            label;
        });
    }
    return _protocolLabel;
}
- (UITextField *)findPwdTextField {
    if (!_findPwdTextField) {
        _findPwdTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(215*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.pwdTextField.frame)+5*DHFlexibleVerticalMutiplier(), 70*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            text.leftViewMode = UITextFieldViewModeAlways;
            UIView * leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            UIImageView * userImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 10*DHFlexibleHorizontalMutiplier(), 15*DHFlexibleHorizontalMutiplier(), 15*DHFlexibleHorizontalMutiplier())];
            userImage.image = IMAGE_NAME(@"wjmm49.png");
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(respondToFindPwdBtn)];
            [leftView addGestureRecognizer:tap];
            [leftView addSubview:userImage];
            text.leftView = leftView;
            text.keyboardType = UIKeyboardTypeDefault;
            text.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            text.textColor = [UIColor whiteColor];
            text.textAlignment = NSTextAlignmentRight;
            text.delegate = self;
            text.text = @"忘记密码";
            text;
        });
    }
    return _findPwdTextField;
}
- (UIButton *)loginBtn {
    if (!_loginBtn) {
        _loginBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(35*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.protocolLabel.frame)+30*DHFlexibleHorizontalMutiplier(), 250*DHFlexibleHorizontalMutiplier(), 40*DHFlexibleHorizontalMutiplier());
            btn.backgroundColor = COLOR_RGB(234, 27, 47, 1);
            btn.layer.cornerRadius = 5*DHFlexibleHorizontalMutiplier();
            [btn setTitle:@"登  录" forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(respondToLoginBtn) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _loginBtn;
}
- (UIButton *)msgLoginBtn {
    if (!_msgLoginBtn) {
        _msgLoginBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(35*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.loginBtn.frame)+20*DHFlexibleHorizontalMutiplier(), 60*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier());
            btn.titleLabel.font = [UIFont systemFontOfSize:13*DHFlexibleHorizontalMutiplier()];
            [btn setTitle:@"短信登陆" forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(respondToMsgLoginBtn) forControlEvents:UIControlEventTouchUpInside];
            
            btn;
        });
    }
    return _msgLoginBtn;
}
- (UIButton *)registerBtn {
    if (!_registerBtn) {
        _registerBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(130*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.loginBtn.frame)+20*DHFlexibleHorizontalMutiplier(), 60*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier());
            btn.titleLabel.font = [UIFont systemFontOfSize:13*DHFlexibleHorizontalMutiplier()];
            [btn setTitle:@"立即注册" forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(respondToRegisterBtn) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _registerBtn;
}

@end
